<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Using arbitrary precision floating point types</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Boost.Numeric.Odeint">
<link rel="up" href="../tutorial.html" title="Tutorial">
<link rel="prev" href="using_matrices_as_state_types.html" title="Using matrices as state types">
<link rel="next" href="self_expanding_lattices.html" title="Self expanding lattices">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../logo.jpg"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="using_matrices_as_state_types.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="self_expanding_lattices.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numeric_odeint.tutorial.using_arbitrary_precision_floating_point_types"></a><a class="link" href="using_arbitrary_precision_floating_point_types.html" title="Using arbitrary precision floating point types">Using
      arbitrary precision floating point types</a>
</h3></div></div></div>
<p>
        Sometimes one needs results with higher precision than provided by the standard
        floating point types. As odeint allows to configure the fundamental numerical
        type, it is well suited to be run with arbitrary precision types. Therefore,
        one only needs a library that provides a type representing values with arbitrary
        precision and the fundamental operations for those values. <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/" target="_top">Boost.Multiprecision</a>
        is a boost library that does exactly this. Making use of <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/" target="_top">Boost.Multiprecision</a>
        to solve odes with odeint is very simple, as the following example shows.
      </p>
<p>
        Here we use <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code>
        as the fundamental value type, which ensures exact computations up to 50
        decimal digits.
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">numeric</span><span class="special">/</span><span class="identifier">odeint</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_dec_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">odeint</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_dec_float_50</span> <span class="identifier">value_type</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">array</span><span class="special">&lt;</span> <span class="identifier">value_type</span> <span class="special">,</span> <span class="number">3</span> <span class="special">&gt;</span> <span class="identifier">state_type</span><span class="special">;</span>
</pre>
<p>
      </p>
<p>
        As exemplary ODE again the lorenz system is chosen, but here we have to make
        sure all constants are initialized as high precision values.
      </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">lorenz</span>
<span class="special">{</span>
    <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span> <span class="keyword">const</span> <span class="identifier">state_type</span> <span class="special">&amp;</span><span class="identifier">x</span> <span class="special">,</span> <span class="identifier">state_type</span> <span class="special">&amp;</span><span class="identifier">dxdt</span> <span class="special">,</span> <span class="identifier">value_type</span> <span class="identifier">t</span> <span class="special">)</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">sigma</span><span class="special">(</span> <span class="number">10</span> <span class="special">);</span>
        <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">R</span><span class="special">(</span> <span class="number">28</span> <span class="special">);</span>
        <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">b</span><span class="special">(</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">8</span> <span class="special">)</span> <span class="special">/</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">3</span> <span class="special">)</span> <span class="special">);</span>

        <span class="identifier">dxdt</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">sigma</span> <span class="special">*</span> <span class="special">(</span> <span class="identifier">x</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">-</span> <span class="identifier">x</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">);</span>
        <span class="identifier">dxdt</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">R</span> <span class="special">*</span> <span class="identifier">x</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">-</span> <span class="identifier">x</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">-</span> <span class="identifier">x</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">*</span> <span class="identifier">x</span><span class="special">[</span><span class="number">2</span><span class="special">];</span>
        <span class="identifier">dxdt</span><span class="special">[</span><span class="number">2</span><span class="special">]</span> <span class="special">=</span> <span class="special">-</span><span class="identifier">b</span> <span class="special">*</span> <span class="identifier">x</span><span class="special">[</span><span class="number">2</span><span class="special">]</span> <span class="special">+</span> <span class="identifier">x</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">*</span> <span class="identifier">x</span><span class="special">[</span><span class="number">1</span><span class="special">];</span>
    <span class="special">}</span>
<span class="special">};</span>
</pre>
<p>
      </p>
<p>
        The actual integration then is straight forward:
      </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">state_type</span> <span class="identifier">x</span> <span class="special">=</span> <span class="special">{{</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">10.0</span> <span class="special">)</span> <span class="special">,</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">10.0</span> <span class="special">)</span> <span class="special">,</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">10.0</span> <span class="special">)</span> <span class="special">}};</span>

<span class="identifier">cout</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span> <span class="number">50</span> <span class="special">);</span>
<span class="identifier">integrate_const</span><span class="special">(</span> <span class="identifier">runge_kutta4</span><span class="special">&lt;</span> <span class="identifier">state_type</span> <span class="special">,</span> <span class="identifier">value_type</span> <span class="special">&gt;()</span> <span class="special">,</span>
        <span class="identifier">lorenz</span><span class="special">()</span> <span class="special">,</span> <span class="identifier">x</span> <span class="special">,</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">0.0</span> <span class="special">)</span> <span class="special">,</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">10.0</span> <span class="special">)</span> <span class="special">,</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">1.0</span> <span class="special">)</span> <span class="special">/</span> <span class="identifier">value_type</span><span class="special">(</span> <span class="number">10.0</span> <span class="special">)</span> <span class="special">)</span> <span class="special">,</span>
        <span class="identifier">streaming_observer</span><span class="special">(</span> <span class="identifier">cout</span> <span class="special">)</span> <span class="special">);</span>
</pre>
<p>
      </p>
<p>
        The full example can be found at <a href="https://github.com/headmyshoulder/odeint-v2/blob/master/examples/multiprecision/lorenz_mp.cpp" target="_top">lorenz_mp.cpp</a>.
        Another example that compares the accuracy of the high precision type with
        standard double can be found at <a href="https://github.com/headmyshoulder/odeint-v2/blob/master/examples/multiprecision/cmp_precision.cpp" target="_top">cmp_precision.cpp</a>.
      </p>
<p>
        Furthermore, odeint can also be run with other multiprecision libraries,
        e.g. <a href="http://gmplib.org/" target="_top">gmp</a>. An example for this is
        given in <a href="https://github.com/headmyshoulder/odeint-v2/blob/master/examples/gmpxx/lorenz_gmpxx.cpp" target="_top">lorenz_gmpxx.cpp</a>.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2015 Karsten Ahnert and Mario Mulansky<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="using_matrices_as_state_types.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="self_expanding_lattices.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
